package com.skcc.ts.nsi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.skcc.ts.nsi.expense.MEExpenseMgmt;
import com.skcc.ts.nsi.project.MEPjtMain;
import com.skcc.ts.nsi.service.SocketClientService;
import com.skcc.ts.nsi.setting.MESetting;
import com.skcc.ts.nsi.user.MEUsrMain;

public class MEMain extends Activity {

	private ImageButton btn_user_mgmt;
	private ImageButton btn_set_mgmt;
	private ImageButton btn_pjt_mgmt;
	private ImageButton btn_expense_mgmt;
	private String myId;
	private String addr;
	private String port;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		doMainStart();// ID, SMS 수신
		doMain();
		initButtons();
	}

	private void doMainStart() {
		Intent receiveI = getIntent();
		boolean sms = false;
		boolean dostart = receiveI.getBooleanExtra("mainstart", false);
		if (dostart) {
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			// sms = receiveI.getBooleanExtra("sms", false);
			// if (sms) {
			// new AlertDialog.Builder(MEMain.this).setTitle("알림").setMessage("정산내역이 등록되었습니다.").setPositiveButton("OK", null).show();
			// }
		}
	}

	private void doMain() {
		Intent receiveI = getIntent();
		boolean redoYN = receiveI.getBooleanExtra("restart", false);
		myId = receiveI.getStringExtra("myId");
		addr = receiveI.getStringExtra("loginAddr");
		port = receiveI.getStringExtra("loginPort");
		if (redoYN) {
			new AlertDialog.Builder(MEMain.this).setTitle("알림").setMessage("등록된 프로젝트가 없습니다").setPositiveButton("OK", null).show();
		}
	}

	private void initButtons() {
		btn_user_mgmt = (ImageButton) findViewById(R.id.btn_user_mgmt);// 사용자관리
		btn_set_mgmt = (ImageButton) findViewById(R.id.btn_set_mgmt);// 설정
		btn_pjt_mgmt = (ImageButton) findViewById(R.id.btn_pjt_mgmt);// 프로젝트관리
		btn_expense_mgmt = (ImageButton) findViewById(R.id.btn_expense_mgmt);// 비용정산관리

		btn_user_mgmt.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent usrI = new Intent(MEMain.this, MEUsrMain.class);
				usrI.putExtra("myId", myId);
				usrI.putExtra("loginAddr", addr);
				usrI.putExtra("loginPort", port);
				usrI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(usrI);
			}
		});

		btn_set_mgmt.setOnClickListener(new View.OnClickListener() {// 설정
					public void onClick(View arg0) {
						Intent setI = new Intent(MEMain.this, MESetting.class);
						startActivity(setI);
					}
				});

		btn_pjt_mgmt.setOnClickListener(new View.OnClickListener() {// 프로젝트 관리
					public void onClick(View arg0) {
						Intent pjtI = new Intent(MEMain.this, SocketClientService.class);
						pjtI.putExtra("id", 31);// 기능분류(프로젝트관리:조회)
						pjtI.putExtra("myId", myId);
						pjtI.putExtra("loginAddr", addr);
						pjtI.putExtra("loginPort", port);
						pjtI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startService(pjtI);
					}
				});

		btn_expense_mgmt.setOnClickListener(new View.OnClickListener() {// 비용관리
					public void onClick(View arg0) {
						Intent expI = new Intent(MEMain.this, MEExpenseMgmt.class);
						expI.putExtra("myId", myId);
						expI.putExtra("loginAddr", addr);
						expI.putExtra("loginPort", port);
						expI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
						startActivity(expI);
					}
				});
	}
}